package engine.graphics.popup;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

import util.ArrayList;
import util.List;
import engine.data.City;
import engine.data.Player;
import engine.data.Terrain;
import engine.data.TerrainType;
import engine.data.Tile;
import engine.data.Unit;
import engine.data.UnitType;
import engine.graphics.MapCanvasEngine;
import engine.graphics.misc.GraphicsCustomFont;

public final class PopupTerrain extends TabPopup {

	private static final byte UNITS_PANEL = 1;

	private static final byte TERRAIN_PANEL = 2;

	private static final byte MISC_PANEL = 3;

	private static int[][][] unitsCursor;

	private Tile tile;

	private short tileX;

	private short tileY;

	private byte vUnitIndex = 0;

	private byte vUnitSubIndex = 0;

	private List units = new ArrayList();

	public Object selectedObject = null;

	private Player player;

	public PopupTerrain(final Player gamePlayer, final Tile selectedTile, final short mapX, final short mapY) {
		tabCursor = new int[][][] { { { 1, 1 }, { 17, 11 } }, { { 20, 1 }, { 28, 11 } }, { { 50, 1 }, { 38, 11 } },
				{ { 90, 1 }, { 23, 11 } } };
		this.player = gamePlayer;
		this.tileX = mapX;
		this.tileY = mapY;
		setTile(selectedTile);
	}

	public void show(final int screenX, final int screenY) {
		Graphics graphics = MapCanvasEngine.canvas.graphics;
		super.show(screenX, screenY, graphics);
		GraphicsCustomFont.drawString(graphics, "[X] Units Terrain Goto", this.x + 2, this.y + 3);
		this.hIndex = UNITS_PANEL;
		showHCursor();
		showPanel();
	}

	public void showMiscPanel() {
		Graphics graphics = MapCanvasEngine.canvas.graphics;
		int j = MARGIN_PANEL_HEIGHT;
		String[] miscCommands = {};
		for (byte i = 0; i < miscCommands.length; i++) {
			GraphicsCustomFont.drawString(graphics, miscCommands[i], this.x + 5, this.y + j);
			j += 10;
		}
		this.panel = MISC_PANEL;
	}

	public void showTerrainPanel() {
		Graphics graphics = MapCanvasEngine.canvas.graphics;
		Terrain terrain = this.tile.terrain;
		StringBuffer sb = new StringBuffer();
		sb.append(TerrainType.getName(terrain.type)).append("\n \nFood          : ").append(terrain.getFP(player))
				.append("\nResource      : ").append(terrain.getPP(player)).append("\nTrade         : ").append(
						terrain.getTP(player, null, tileX, tileY)).append("\n \nMovement Cost : ").append(
						terrain.getMPC() / 3).append("\nDefense bonus : ").append(terrain.getDB()).append(" %");
		GraphicsCustomFont.drawString(graphics, sb.toString(), this.x + 5, this.y + MARGIN_PANEL_HEIGHT);
		this.panel = TERRAIN_PANEL;
	}

	public void showUnitsPanel() {
		Graphics graphics = MapCanvasEngine.canvas.graphics;
		StringBuffer sb = new StringBuffer();
		int j = 18;
		byte max = (byte) Math.min(this.units.size(), ((vUnitSubIndex + 1) * 6));
		Image image;
		City city;
		Unit unit;
		String name;
		for (byte i = (byte) (vUnitSubIndex * 6); i < max; i++) {
			Object element = this.units.get(i);
			if (element.getClass().equals(City.class)) {
				city = (City) element;
				image = city.image;
				name = city.name;
			} else {
				unit = (Unit) element;
				image = unit.getImage();
				sb = new StringBuffer();
				sb.append(UnitType.getName(unit.type)).append(" (").append(UnitType.getAP(unit.type)).append("/")
						.append(UnitType.getDP(unit.type)).append("/").append(unit.mp / 3).append(".").append(
								unit.mp % 3).append(")");
				name = sb.toString();
			}
			graphics.drawImage(image, this.x + 4, this.y + j, Graphics.TOP | Graphics.LEFT);
			GraphicsCustomFont.drawString(graphics, name, this.x + 25, this.y + 4 + j);
			j += 18;
		}
		if (this.units.size() > 6)
			GraphicsCustomFont.drawString(graphics, "...", this.x + 115, this.y + 125);
		this.panel = UNITS_PANEL;
	}

	public void keyPressed(final int key) {
		int action = MapCanvasEngine.canvas.getGameAction(key);
		if (action != 0)
			switch (action) {
				case Canvas.DOWN:
					if (this.panel == UNITS_PANEL)
						vUnitIndex = moveCursor(unitsCursor, vUnitIndex, (byte) 1, (byte) 7);
					break;
				case Canvas.UP:
					if (this.panel == UNITS_PANEL)
						vUnitIndex = moveCursor(unitsCursor, vUnitIndex, (byte) -1, (byte) 7);
					break;
				case Canvas.LEFT:
					showNextTab((byte) -1);
					break;
				case Canvas.RIGHT:
					showNextTab((byte) 1);
					break;
				case Canvas.FIRE:
					if (this.panel == UNITS_PANEL) {
						if (vUnitIndex % 7 == 6 || (vUnitIndex == unitsCursor.length - 1 && this.units.size() > 6)) {
							vUnitIndex += 7;
							vUnitSubIndex++;
							if (vUnitSubIndex * 6 > this.units.size()) {
								vUnitSubIndex = 0;
								vUnitIndex = 6;
							}
							showPanel();
							if (vUnitIndex >= unitsCursor.length)
								vUnitIndex = (byte) (unitsCursor.length - 1);
							showVCursor(unitsCursor, vUnitIndex);
						} else {
							if (!hCursorScope) {
								Object element = this.units.get((vUnitSubIndex * 6) + vUnitIndex % 7);
								selectedObject = element;
								this.hide();
							}
						}
					}
					if (this.panel == CLOSE_PANEL) {
						this.hide();
					}
					break;
				default:
					break;
			}
	}

	protected void showPanel() {
		super.showPanel();
		switch (this.hIndex) {
			case TERRAIN_PANEL:
				showTerrainPanel();
				break;
			case UNITS_PANEL:
				showUnitsPanel();
				break;
			case MISC_PANEL:
				showMiscPanel();
				break;
			default:
				break;
		}
		this.repaint();
	}

	private void setTile(final Tile selectedTile) {
		this.tile = selectedTile;
		if (this.tile.city != null)
			this.units.add(this.tile.city);
		if (tile.units != null && !tile.hasEnnemyUnits(player)) {
			int vSize = tile.units.size();
			for (short i = 0; i < vSize; i++)
				this.units.add(tile.units.get(i));
		}
		unitsCursor = initCursor((byte) (this.units.size()), (byte) 18);
	}

}
